﻿using System.IO;
using Db4objects.Db4o;

namespace Hover.Common.Repositories
{

    public class DB4OHelper
    {
        static readonly object padlock = new object();

        // static object container variable
        static IObjectContainer _db = null;

        private static string _dbPath = @".\App_Data";
        //static string _dbPath = System.Configuration.ConfigurationManager.ConnectionStrings["ObjectStore"].ConnectionString;
        public static string DBPath
        {
            get
            {
                return _dbPath;
            }
            set
            {
                _dbPath = value;
            }
        }

        public static IObjectContainer Container
        {
            get
            {
                lock (padlock)
                {
                    if (_db == null)
                    {

                        //check to see if this is pointing to data directory
                        //change as you need btw
                        if (_dbPath.Contains("|DataDirectory|"))
                        {

                            //we know, then, that this is a web project
                            //and HttpContext is hopefully not null...

                            _dbPath = _dbPath.Replace("|DataDirectory|", "");
                            string appDir = @"c:\Temp";
                            //string appDir = HttpContext.Current.Server.MapPath("~/App_Data/");
                            _dbPath = Path.Combine(appDir, _dbPath);
                        }

                        _db = Db4oFactory.OpenFile(_dbPath);
                    }
                    return _db;
                }
            }
        }

        public static void CloseContainer()
        {
            if (_db != null)
            {
                _db.Close();
            }
            _db = null;
        }

    }
}
